<?php

use Illuminate\Database\Seeder;

class AdministratorMenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
//        $paperExamQuestions = \App\Services\Designer\Model\PaperExamQuestion::query()->get();
//        foreach ($paperExamQuestions as $paperExamQuestion){
//            $paperExamQuestion->question_id = $paperExamQuestion->question_answer['id'];
//            $paperExamQuestion->save();
//        }
//
//        $questions = \App\Services\Designer\Model\Question::query()->get();
//        foreach ($questions as $question){
//            $question->exam_count = \App\Services\Designer\Model\PaperExamQuestion::query()->where('question_id',$question->id)->count();
//            $question->error_count = \App\Services\Designer\Model\PaperExamQuestion::query()->where('question_id',$question->id)->where('status',\App\Services\Designer\Model\PaperExamQuestion::ERROR)->count();
//            $question->save();
//        }

        $menus = [
            [
                'title' => '首页',
                'key' => 'Home',
                'icon' => 'icon-monitor',
            ],
            [
                'title' => '系统',
                'key' => 'system',
                'icon' => 'icon-cog',
                'children' => [
                    [
                        'title' => '配置',
                        'key' => 'Setting',
                        'permission' => 'setting',
                    ],
                ],
            ],
            [
                'title' => '管理员管理',
                'key' => 'administrator',
                'icon' => 'icon-head',
                'children' => [
                    [
                        'title' => '管理员',
                        'key' => 'Administrator',
                        'permission' => 'administrator',
                    ],
                    [
                        'title' => '管理员角色',
                        'key' => 'AdministratorRole',
                        'permission' => 'administrator_role',
                    ],
                ],
            ],
            [
                'title' => '设计师管理',
                'key' => 'designer',
                'icon' => 'icon-eye',
                'children' => [
                    [
                        'title' => '设计师',
                        'key' => 'Designer',
                        'permission' => 'designer',
                    ],
                    [
                        'title' => '设计之星',
                        'key' => 'DesignerStar',
                        'permission' => 'designer_star',
                    ],
                ],
            ],
            [
                'title' => 'Banner管理',
                'key' => 'banner',
                'icon' => 'icon-map',
                'children' => [
                    [
                        'title' => 'Banner图',
                        'key' => 'Banner',
                        'permission' => 'banner',
                    ],
                ],
            ],
            [
                'title' => '材料图库管理',
                'key' => 'materialGallery',
                'icon' => 'icon-bar-graph-2',
                'children' => [
                    [
                        'title' => '材料大类',
                        'key' => 'MaterialParent',
                        'permission' => 'materialparent',
                    ],
                    [
                        'title' => '材料',
                        'key' => 'Material',
                        'permission' => 'material',
                    ],
                    [
                        'title' => '材料图',
                        'key' => 'MaterialGallery',
                        'permission' => 'material_gallery',
                    ],
                ],
            ],
            [
                'title' => '系列图库管理',
                'key' => 'seriesGallery',
                'icon' => 'icon-layers',
                'children' => [
                    [
                        'title' => '系列画报',
                        'key' => 'SeriesGallery',
                        'permission' => 'series_gallery',
                    ],
                    [
                        'title' => '系列图库',
                        'key' => 'NewSeriesGallery',
                        'permission' => 'new_series_gallery',
                    ],
                ],
            ],
            [
                'title' => 'CAD图库管理',
                'key' => 'cadGallery',
                'icon' => 'icon-book',
                'children' => [
                    [
                        'title' => 'CAD图',
                        'key' => 'CadGallery',
                        'permission' => 'cad_gallery',
                    ],
                ],
            ],
            [
                'title' => '经典案例管理',
                'key' => 'classicCase',
                'icon' => 'icon-image',
                'children' => [
                    [
                        'title' => '经典案例',
                        'key' => 'ClassicCase',
                        'permission' => 'classic_case',
                    ],
                ],
            ],
            [
                'title' => '需求管理',
                'key' => 'demand',
                'icon' => 'icon-heart',
                'children' => [
                    [
                        'title' => '设计师俱乐部需求',
                        'key' => 'DesignerDemand',
                        'permission' => 'designer_demand',
                    ],
                    [
                        'title' => 'ERP专卖店需求',
                        'key' => 'ShopDemand',
                        'permission' => 'shop_demand',
                    ],
                ],
            ],
            [
                'title' => '大概念PDF管理',
                'key' => 'manual',
                'icon' => 'icon-book',
                'children' => [
                    [
                        'title' => '大概念PDF',
                        'key' => 'Manual',
                        'permission' => 'manual',
                    ],
                ],
            ],
            [
                'title' => '各页面图片管理',
                'key' => 'layout',
                'icon' => 'icon-image',
                'children' => [
                    [
                        'title' => '各页面图片',
                        'key' => 'Layout',
                        'permission' => 'layout',
                    ],
                ],
            ],
            [
                'title' => '视频管理',
                'key' => 'video',
                'icon' => 'h-icon-loading',
                'children' => [
                    [
                        'title' => '视频',
                        'key' => 'Video',
                        'permission' => 'video',
                    ],
                ],
            ],
            [
                'title' => '小概念PDF管理',
                'key' => 'testReport',
                'icon' => 'icon-book',
                'children' => [
                    [
                        'title' => '小概念PDF',
                        'key' => 'TestReport',
                        'permission' => 'test_report',
                    ],
                ],
            ],
            [
                'title' => '常见设计问题管理',
                'key' => 'designProblem',
                'icon' => 'h-icon-help-solid',
                'children' => [
                    [
                        'title' => '常见设计问题',
                        'key' => 'DesignProblem',
                        'permission' => 'design_problem',
                    ],
                ],
            ],
            [
                'title' => '参考图库管理',
                'key' => 'referenceGallery',
                'icon' => 'icon-image',
                'children' => [
                    [
                        'title' => '参考图库',
                        'key' => 'ReferenceGallery',
                        'permission' => 'reference_gallery',
                    ],
                ],
            ],
            [
                'title' => '评论管理',
                'key' => 'reply',
                'icon' => 'icon-image',
                'children' => [
                    [
                        'title' => '评论',
                        'key' => 'Reply',
                        'permission' => 'reply',
                    ],
                ],
            ],
            [
                'title' => '题库管理',
                'key' => 'question',
                'icon' => 'icon-image',
                'children' => [
                    [
                        'title' => '题目',
                        'key' => 'Question',
                        'permission' => 'question',
                    ],
                ],
            ],
            [
                'title' => '公告管理',
                'key' => 'gonggao',
                'icon' => 'icon-map',
                'children' => [
                    [
                        'title' => '公告',
                        'key' => 'Gonggao',
                        'permission' => 'gonggao',
                    ],
                ],
            ],
            [
                'title' => '设计师报表管理',
                'key' => 'performance',
                'icon' => 'icon-map',
                'children' => [
                    [
                        'title' => '设计师数据',
                        'key' => 'Performance',
                        'permission' => 'performance',
                    ],
                ],
            ],
            [
                'title' => '空间方案库管理',
                'key' => 'spaceScheme',
                'icon' => 'icon-bar-graph-2',
                'children' => [
                    [
                        'title' => '空间方案风格',
                        'key' => 'SpaceSchemeStyle',
                        'permission' => 'spaceschemestyle',
                    ],
                    [
                        'title' => '空间方案系列',
                        'key' => 'SpaceSchemeSeries',
                        'permission' => 'spaceschemeseries',
                    ],
                    [
                        'title' => '空间方案',
                        'key' => 'SpaceScheme',
                        'permission' => 'spacescheme',
                    ],
                ],
            ],
            [
                'title' => '微信文章管理',
                'key' => 'wxCaseArticle',
                'icon' => 'icon-bar-graph-2',
                'children' => [
                    [
                        'title' => '微信文章',
                        'key' => 'WxCaseArticle',
                        'permission' => 'wxcasearticle',
                    ],
                ],
            ],
            [
                'title' => '自助报价管理',
                'key' => 'selfServiceQuotation',
                'icon' => 'icon-bar-graph-2',
                'children' => [
                    [
                        'title' => '定制风格',
                        'key' => 'CustomStyle',
                        'permission' => 'custom_style',
                    ],
                    [
                        'title' => '自助报价',
                        'key' => 'SelfServiceQuotation',
                        'permission' => 'self_service_quotation',
                    ],
                ],
            ],
            [
                'title' => '设计服务管理',
                'key' => 'designServe',
                'icon' => 'icon-bar-graph-2',
                'children' => [
                    [
                        'title' => 'banner图管理',
                        'key' => 'designServebanner',
                        'permission' => 'design_serve_banner',
                    ],
                ],
            ],
        ];
        // DB::delete('truncate table kd_administrator_menus');
        foreach ($menus as $index => $menu) {
            $sort = ($index + 1) * 10;
            $model = $this->create($menu, 0, $sort);
            if ($model === false) {
                continue;
            }
            $children = $menu['children'] ?? [];
            if (!$children) {
                continue;
            }
            foreach ($children as $i => $item) {
                $this->create($item, $model->id, $sort * 10 + ($i + 1) * 10);
            }
        }
    }

    protected function create($menu, $parentId = 0, $sort = 0)
    {
        $exists = \App\Models\AdministratorMenu::query()->where('name', $menu['title'])->where('url', $menu['key'])->exists();
        if ($exists) {
            return \App\Models\AdministratorMenu::query()->where('name', $menu['title'])->where('url', $menu['key'])->firstOrFail();
        }
        return \App\Models\AdministratorMenu::create([
            'parent_id' => $parentId,
            'name' => $menu['title'],
            'url' => $menu['key'] ?? '',
            'permission' => $menu['permission'] ?? '',
            'icon' => $menu['icon'] ?? '',
            'is_super' => $menu['super'] ?? 0,
            'sort' => $sort,
        ]);
    }
}
